
package com.eshore.ezone.utils;

/**
 * The Class LogUtils for log print, it can help us
 * easy to trace our codes or logics in the project .
 * 
 * @author zhao xiaogang
 * @time   2012.03.03
 */
public class LogUtils {

    /** The Constant VERBOSE. */
    private final static int VERBOSE = 0;

    /** The Constant DEBUG. */
    private final static int DEBUG = 1;

    /** The Constant INFO. */
    private final static int INFO = 2;

    /** The Constant WARN. */
    private final static int WARN = 3;

    /** The Constant ERROR. */
    private final static int ERROR = 4;

    /** The Constant DEFAULT_LEVEL. */
    private final static int DEFAULT_LEVEL = -1;

    /** The clazz. */
    private final String clazz;

    /** The level. */
    private int level;

    /** The Constant TAG. */
    private static final String TAG = "LogUtils";

    /**
     * Gets the debug log.
     *
     * @param clazz the clazz
     * @param l the l
     * @return the debug log
     */
    public static LogUtils getDebugLog(Class<?> clazz, int l) {
        LogUtils log = new LogUtils(clazz);
        log.level = l;
        return log;
    }

    /**
     * Gets the log.
     *
     * @param clazz the clazz
     * @return the log
     */
    public static LogUtils getLog(Class<?> clazz) {
        return new LogUtils(clazz);
    }

    /**
     * Instantiates a new log utils.
     *
     * @param clazz the clazz
     */
    public LogUtils(Class<?> clazz) {
        this.clazz = "[" + clazz.getSimpleName() + "] ";
        level = DEFAULT_LEVEL;
    }

    /**
     * Verbose.
     *
     * @param message the message
     */
    public void verbose(String message) {
        verbose(message, null);
    }

    /**
     * Debug.
     *
     * @param message the message
     */
    public void debug(String message) {
        debug(message, null);
    }

    /**
     * Info.
     *
     * @param message the message
     */
    public void info(String message) {
        info(message, null);
    }

    /**
     * Warn.
     *
     * @param message the message
     */
    public void warn(String message) {
        warn(message, null);
    }

    /**
     * Error.
     *
     * @param message the message
     */
    public void error(String message) {
        error(message, null);
    }

    /**
     * Verbose.
     *
     * @param message the message
     * @param t the t
     */
    public void verbose(String message, Throwable t) {
        if (VERBOSE < level) {
            return;
        }
        if (message != null) {
            android.util.Log.v(TAG, clazz+" Line: "+getLineNumber()+" : "+message);
        }
        if (t != null) {
            android.util.Log.v(TAG, clazz+" Line: "+getLineNumber()+" : "+t.toString());
        }
    }

    /**
     * Debug.
     *
     * @param message the message
     * @param t the t
     */
    public void debug(String message, Throwable t) {
        if (DEBUG < level) {
            return;
        }
        if (message != null) {
            android.util.Log.d(TAG, clazz+" Line: "+getLineNumber()+" : "+message);
        }
        if (t != null) {
            android.util.Log.d(TAG, clazz+" Line: "+getLineNumber()+" : "+t.toString());
        }
    }

    /**
     * Info.
     *
     * @param message the message
     * @param t the t
     */
    public void info(String message, Throwable t) {
        if (INFO < level) {
            return;
        }
        if (message != null) {
            android.util.Log.i(TAG, clazz+" Line: "+getLineNumber()+" : "+message);
        }
        if (t != null) {
            android.util.Log.i(TAG, clazz+" Line: "+getLineNumber()+" : "+t.toString());
        }
    }

    /**
     * Warn.
     *
     * @param message the message
     * @param t the t
     */
    public void warn(String message, Throwable t) {
        if (WARN < level)
            return;
        if (message != null) {
            android.util.Log.w(TAG, clazz + " Line: " + getLineNumber() + " : " + message);
        }
        if (t != null) {
            android.util.Log.w(TAG, clazz + " Line: " + getLineNumber() + " : " + t.toString());
        }
    }

    /**
     * Error.
     *
     * @param message the message
     * @param t the t
     */
    public void error(String message, Throwable t) {
        if (ERROR < level) {
            return;
        }
        if (message != null) {
            android.util.Log.e(TAG, clazz+" Line: "+getLineNumber()+" : "+message);
        }
        if (t != null) {
            android.util.Log.e(TAG, clazz+" Line: "+getLineNumber()+" : "+t.toString());
        }
    }

    /**
     * Gets the line number.
     * 
     * @return the line number
     */
    private int getLineNumber() {
        return Thread.currentThread().getStackTrace()[5].getLineNumber();
    }
}
